package com.guigusuqi.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.guigusuqi.app.entity.DTO.MedicalCardDTO;
import com.guigusuqi.app.entity.MedicalCard;
import com.guigusuqi.app.mapper.MedicalCardMapper;
import com.guigusuqi.app.service.MedicalCardService;
import com.guigusuqi.commonutils.vo.Result;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author suqi
 * @since 2022-04-06
 */
@Service
public class MedicalCardServiceImpl extends ServiceImpl<MedicalCardMapper, MedicalCard> implements MedicalCardService
{
    @Resource
    private MedicalCardMapper medicalCardMapper;

    @Override
    public Result getMedicalCard(String openid,MedicalCard medicalCard)
    {
        // 先从数据库里面查有没有这张卡
        LambdaQueryWrapper<MedicalCard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MedicalCard::getId,medicalCard.getId());
        queryWrapper.eq(MedicalCard::getIdCard,medicalCard.getIdCard());
        queryWrapper.eq(MedicalCard::getName,medicalCard.getName());
        queryWrapper.eq(MedicalCard::getMobile,medicalCard.getMobile());
        MedicalCard medicalCard1 = medicalCardMapper.selectOne(queryWrapper);
        if(medicalCard1 == null)
        {
            return Result.fail().message("输入的就诊卡信息有误");
        }else
        {
            int insert = medicalCardMapper.saveUserAndCard(openid,medicalCard.getId());
            if(insert >= 1)
            {
                return Result.success().data("rows",medicalCard1);
            }else
            {
                return Result.fail().message("绑定失败！");
            }
        }
    }

    @Override
    public Result addMedicalCard(String openid, MedicalCardDTO medicalCardDTO)
    {
        if (StringUtils.isEmpty(medicalCardDTO.getMobile()))
        {
            return Result.fail().message("电话号码不能为空!");
        }
        List<MedicalCard> medicalCards = medicalCardMapper.selectList(null);
        for(MedicalCard medicalCard1 : medicalCards)
        {
            if(medicalCard1.getMobile().equals(medicalCardDTO.getMobile()))
            {
                return Result.fail().message("一个电话号码只能添加一张就诊卡！");
            }
        }
        MedicalCard medicalCard = new MedicalCard();
        BeanUtils.copyProperties(medicalCardDTO,medicalCard);
        int insert = medicalCardMapper.insert(medicalCard);
        if(insert >= 1)
        {
            int i = medicalCardMapper.saveUserAndCard(openid, medicalCard.getId());
            if(i >= 1)
            {
                return Result.success().message("添加就诊卡并绑定成功!");
            }else
            {
                return Result.success().message("添加就诊卡成功，绑定失败!");
            }
        }else
        {
            return Result.fail().message("建卡失败");
        }
    }

    @Override
    public Result findMedicalCardList(String openid)
    {
        List<MedicalCard> medicalCardList = medicalCardMapper.findUserMedicalCardByOpenid(openid);
        return Result.success().data("rows",medicalCardList);
    }
}
